<template>
  <div>
    <login-canvas />
    <div class="login-container">
      <div
        class="login-left-banner"
        style="
        opacity: 0.5;
        background-image: url('https://cdn.web-global.fds.api.mi-img.com/mcfe--mi-account/static/static/media/banner.a0e6fbf0.jpg');
      "
      ></div>
      <div class="login-sider-container">
        <div class="sider-cent">
          <div class="sider-header">
            <!-- <img src="https://vitejs.cn/logo.svg" alt="" /> -->
            <img src="https://vue3js.cn/docs/logo.png" alt="" />
            <img class="eleplus" src="../../assets/images/eleplus.svg" alt="" />
          </div>
          <el-form :rules="loginRules" :model="userInfo" ref="loginForm">
            <el-form-item prop="username">
              <span @click="$refs.userNameRef.focus()" :class="nameClass"
                >请输入账号/手机号</span
              >
              <el-input
                v-model.trim="userInfo.username"
                @focus="habdleUserName(true)"
                @blur="habdleUserName(false)"
                ref="userNameRef"
              ></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <span @click="$refs.passWordRef.focus()" :class="passClass"
                >请输入密码</span
              >
              <el-input
                v-model.trim="userInfo.password"
                ref="passWordRef"
                @focus="habdleUserPass(true)"
                @blur="habdleUserPass(false)"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <div>
                登录表示您已同意<a style="color: #409eff; cursor: pointer"
                  >《服务条款》</a
                >
              </div>
              <el-button type="primary" @click="submitForm" class="submit-btn"
                >立即登录</el-button
              >
              <div>默认账号: admin 密码:123456</div>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import LoginCanvas from '../../components/LoginCanvas.vue'
import { LoginIndex } from '../../api/request'
import axios from 'axios'
export default {
  data() {
    return {
      userInfo: {
        username: '',
        password: '',
      },
      nameClass: '',
      passClass: '',
      // 效验规则
      loginRules: {
        username: [
          { required: 'true', message: '账户不能为空', trigger: 'blur' },
        ],
        password: [
          { required: 'true', message: '密码不能为空', trigger: 'blur' },
          { min: 3, max: 12, message: '请填写正确密码长度', trigger: 'blur' },
        ],
      },
    }
  },
  components: {
    LoginCanvas,
  },
  methods: {
    habdleUserName(flag) {
      console.log(flag)
      if (flag) this.nameClass = 'name-right'
      else {
        const { username } = this.userInfo
        if (!username) this.nameClass = ''
      }
    },
    habdleUserPass(flag) {
      if (flag) this.passClass = 'pass-right'
      else {
        const { password } = this.userInfo
        if (!password) this.passClass = ''
      }
    },
    habdleUserName(flag) {
      if (flag) this.nameClass = 'name-right'
      else {
        const { username } = this.userInfo
        if (!username) this.nameClass = ''
      }
    },
    habdleUserPass(flag) {
      if (flag) this.passClass = 'pass-right'
      else {
        const { password } = this.userInfo
        if (!password) this.passClass = ''
      }
    },
    // 点击登录按钮
    async submitForm() {
      const { username, password } = this.userInfo
      // 登录前验证
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          // 使用封装接口
          // LoginIndex(this.userInfo).then((res) => {
          //   console.log(res)

          // })
          // 直接使用axios
          axios({
            method: 'post',
            url: 'http://leju.bufan.cloud/lejuAdmin/index/login',
            data: this.userInfo,
          }).then((res) => {
            if ((res.code = 20000)) {
              // 登录成功后
              // 1.提示登录成功  2.保存用户的token  3.跳转到首页
              this.$message.success('登录成功')
              localStorage.setItem('token', res.data.data.token)
              this.$router.replace('/index')
            } else {
              this.$message.success('登录失败,请检查账号或密码')
            }
            console.log(res)
          })
          // 登录请求发送
        } else {
          this.$message.error('请填写账号或密码')
        }
      })
    },
  },
}
</script>

<style scoped lang="scss">
.instructions {
  position: absolute;
  z-index: 1;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #fff;
  letter-spacing: 2px;
  pointer-events: none;
}

canvas {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 1;
  width: 100%;
  height: 100vh;
}
.login-container {
  position: relative;
  width: 100%;
  min-height: 100vh;
  background: #000;
  .login-left-banner {
    position: absolute;
    left: 0;
    top: 0;
    width: 375px;
    height: 100%;
    background-position: 50%;
    background-size: cover;
  }
  .login-sider-container {
    position: relative;
    margin-left: 375px;
    width: calc(100% - 375px);
    height: 100vh;
    .sider-header {
      padding: 20px;
      display: flex;
      align-items: center;
      justify-content: center;
      img {
        width: 48px;
        height: 48px;
        margin-right: 12px;
      }
      .eleplus {
        width: 128px;
        height: 48px;
      }
    }
    .sider-cent {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      text-align: left;
      border-radius: 4px;
      position: relative;
      z-index: 1;
      background: rgba(255, 255, 255, 0.9);
      padding: 40px 45px 20px;
      width: 450px;
      // box-shadow: 0 20px 50px 0 hsl(0deg 0% 64% / 10%);
      .el-form-item {
        position: relative;
        ::v-deep .el-input__inner {
          padding-right: 160px;
        }
        span {
          transition: left 0.3s ease-in;
          position: absolute;
          left: 20px;
          top: 50%;
          transform: translateY(-50%);
          font-size: 16px;
          color: #5d9aff;
          z-index: 1;
          &.name-right {
            left: 310px;
            font-size: 12px;
          }
          &.pass-right {
            left: 350px;
            font-size: 12px;
          }
        }
      }
      .submit-btn {
        width: 100%;
      }
    }
  }
}
</style>
